<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category AdminController.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/4/27  15:03
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Store;
use App\Http\Requests\Admin\Update;
use App\Models\AdminLog;
use App\Services\AdminService;
use App\Services\RoleService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use App\Models\RoleUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    /**
     * @var string
     */
    protected $_model = 'admin';

    /**
     * @var AdminService
     */
    protected $adminService;

    /**
     * @var RoleService
     */
    protected $roleService;

    /**
     * AdminController constructor.
     */
    public function __construct()
    {
        $this->adminService = AdminService::getInstance();
        $this->roleService  = RoleService::getInstance();
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  10:58
     */
    public function index(Request $request)
    {
        $role = $this->roleService->getAll();
        return view('admin.' . $this->_model . '.index', compact('request', 'role'));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/3/15  16:55
     */
    public function getList(Request $request)
    {
        $responseData = $this->adminService->getList($request);
        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  10:57
     */
    public function create(Request $request)
    {
        $role = $this->roleService->getAll();
        return view('admin.' . $this->_model . '.create', compact('request', 'role'));
    }

    /**
     * @param Store $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/5/25  15:36
     */
    public function store(Store $request)
    {
        $responseData = $this->adminService->addData($request);
        return response()->json($responseData);
    }


    /**
     * @param Request $request
     * @param $id
     * @return Application|Factory|View
     * @Depiction
     * @Author hwy
     * @Date 2021/5/25  15:36
     */
    public function edit(Request $request, $id)
    {
        $data = $this->adminService->getOne($id);
        $role = $this->roleService->getAll();
        return view('admin.' . $this->_model . '.edit', compact('request', 'role', 'data'));
    }

    /**
     * @param Update $request
     * @return array
     * @Depiction
     * @Author hwy
     * @Date 2021/5/25  15:36
     */
    public function update(Update $request)
    {
        $edit = $this->adminService->editData($request);

        if ($edit != false) {
            return ['code' => 0, 'message' => '提交成功'];
        } else {
            return ['code' => -1, 'message' => '提交失败'];
        }
    }

    /**
     * @param Request $request
     * @return array
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  14:06
     */
    public function update_password(Request $request)
    {
        $user = Auth::guard('admin')->user();

        if (!Hash::check($request->old_password, $user->password)) {
            return ['code' => -1, 'message' => '原密码错误'];
        }
        $rules     = [
            'password' => 'required|between:6,18',
        ];
        $messages  = [
            'password.required' => '密码不能为空！',
            'password.between'  => '密码长度在6-18个字符之间！',
        ];
        $validator = Validator::make(['password' => $request->password], $rules, $messages);

        if ($validator->fails()) {
            return ['code' => -1, 'message' => $validator->errors()->first()];
        }
        $res['password'] = bcrypt($request->password);
        $edit            = $result = $this->adminService->where('id', $user->id)->update($res);
        if ($edit != false) {
            return ['code' => 0, 'message' => '编辑成功'];
        } else {
            return ['code' => -1, 'message' => '编辑失败'];
        }
    }

    /**
     * @param Request $request
     * @return array
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  14:07
     */
    public function destroy(Request $request)
    {
        $ids = explode(',', $request->id);
        if (in_array(1, $ids)) {
            return ['code' => -1, 'message' => '超级管理员不能删除'];
        }

        $edit = $this->adminService->delData($ids);
        if ($edit != false) {
            return ['code' => 0, 'message' => '删除成功'];
        } else {
            return ['code' => -1, 'message' => '删除失败'];
        }
    }


}
